﻿using Qiniu.Http;
using Qiniu.Storage;
using Qiniu.Util;
using System;
using System.IO;
using System.Security.Policy;

namespace VOL.WebApi
{
    public class QiNiu
    {
        /// <summary>
        /// NuGet引用： Qiniu(8.3.1)
        /// 七牛云上传普通文件
        /// </summary>
        /// 6YG3I_h8ApXgK8KIvhZV9RAcyKfjAb7QSzVXkNOX
        /// PrasWpNTBO6H_lXlbvdQ6Vf0GGQ4ORTWsHB1b5Af
        public static string QiNiuUploadFile(string url)
        {

            //请到官网申请七牛云账号、秘钥(申请位置：点击头像，秘钥管理)
            //var accessKey = "6YG3I_h8ApXgK8KIvhZV9RAcyKfjAb7QSzVXkNOX";
            //var secretKey = "PrasWpNTBO6H_lXlbvdQ6Vf0GGQ4ORTWsHB1b5Af";
            //https://localhost:44376/GeneratedQRCode/qrcode_20250111_110430_49f11d3a-d636-484e-9647-db15d74fa01f.png


            //https://localhost:44376/GeneratedQRqrcode_20250111_131855_ec5e0cd4-fc06-4716-9edd-6218b1045fb9.png

            //GeneratedQRqrcode_20250111_131548_4a2e0256-78b3-48ba-9837-c3d2cb6cff64.png
            //eneratedQRqrcode_20250111_131548_4a2e0256-78b3-48ba-9837-c3d2cb6cff64.png
            var accessKey = QiNiuDTO.AccessKey;
            var secretKey = QiNiuDTO.SecretKey;

            var tmpFileName = url.Substring(24);

            //保存到七牛云上的文件目录
           // var tmpFileName = url.Substring(75);// "11.doc";// "44.jpg";
            var tmpUrl = "Test/" + tmpFileName; //上传到七牛云的地址目录，注意: /
                             
            //D:\SmartHealthcare\SmartMedicalAPI\Net6.SqlSugar\VOL.WebApi\wwwroot\images\20241212195017954.mp3 20241212195017954.mp3
            string filePath = "D:\\BitterCandy\\factory\\Factory\\src\\Factory.Web\\wwwroot\\qrcode\\" + tmpFileName;
            // 存储空间名
            string Bucket = "dajiaohao";
            // 设置上传策略
            PutPolicy putPolicy = new PutPolicy();
            // 设置要上传的目标空间
            putPolicy.Scope = QiNiuDTO.Bucket;

            // 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖)，请使用 SCOPE = "BUCKET:KEY"
            // putPolicy.Scope = bucket + ":" + saveKey;

            // 上传策略有效期(对应于生成的凭证的有效期，单位:秒)
            putPolicy.SetExpires(3600);
            // 上传到云端多少天后自动删除该文件，如果不设置（即保持默认默认）则不删除
            putPolicy.DeleteAfterDays = 100;

            // 生成上传token
            Mac mac = new Mac(QiNiuDTO.AccessKey, QiNiuDTO.SecretKey);
            string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString()); 

            Config config = new Config();
            // 设置上传区域  [CN_East:华东] [CN_South:华南] [CN_North:华北] [US_North:北美]
            config.Zone = Qiniu.Storage.Zone.ZONE_CN_North;
            // 设置 http 或者 https 上传
            config.UseHttps = true;
            config.UseCdnDomains = true;
            config.ChunkSize = ChunkUnit.U512K;

            // 表单上传
            FormUploader target = new FormUploader(config);
            HttpResult result = target.UploadFile(filePath, tmpUrl, token, null);
            Console.WriteLine("返回结果： " + result.ToString());

            var imgUrl = QiNiuDTO.ImgUrl + tmpUrl;

            var DeleteUrl = "D:\\BitterCandy\\factory\\Factory\\src\\Factory.Web\\wwwroot\\qrcode\\"+ tmpFileName;

        //
        http://spwllv7f8.hb-bkt.clouddn.com/Test/qrcode_20250111_111730_79c0cf8d-1950-4d0d-a5d4-d5bcbe415da9.png

            //http://spwllv7f8.hb-bkt.clouddn.com/Test/eneratedQRqrcode_20250111_131239_1f84fc64-124f-4de1-b6fa-409a53f54ab2.png
            //: 'D:\BitterCandy\factory\Factory\src\Factory.Web\wwwroot\qrcode\https:\localhost:44376\GeneratedQRCode\qrcode_20250111_111626_2820faa9-c634-4c97-ad48-50c99ff9182f.png'

            //http://spwllv7f8.hb-bkt.clouddn.com/Test/qrcode_20250111_111901_27691163-587d-4dc0-85a4-b62a52598499.png
            File.Delete(DeleteUrl);


            Console.WriteLine(imgUrl);
            return (imgUrl);


        }


        /// <summary>
        /// NuGet引用： Qiniu(8.3.1)
        /// 七牛云删除文件
        /// </summary>
        public static void QiNiuDeleteFile(string url)
        {
            //请到官网申请七牛云账号、秘钥
            var accessKey = QiNiuDTO.AccessKey;
            var secretKey = QiNiuDTO.SecretKey;

            // 存储空间名
            string Bucket = QiNiuDTO.Bucket;

            //http://spwllv7f8.hb-bkt.clouddn.com/Test/qrcode_20250112_181852_decd3e46-6aae-4601-b54e-88b95a72b866.png
            //保存到七牛云上的文件目录
            //string saveKey = "images/44.jpg";
            var saveKey = url.Substring(36);

            //设置上传区域
            Config config = new Config();
            config.Zone = Qiniu.Storage.Zone.ZONE_CN_East;//ZONE_CN_North
            Mac mac = new Mac(accessKey, secretKey);

            BucketManager bucketManager = new BucketManager(mac, config);
            HttpResult deleteRet = bucketManager.Delete(Bucket, saveKey);
            if (deleteRet.Code == (int)HttpCode.OK)
            {
                Console.WriteLine("删除成功： " + deleteRet.ToString());
            }
            else
            {
                Console.WriteLine("删除失败： " + deleteRet.ToString());
            }

            Console.WriteLine(deleteRet);

        }

        /// <summary>
        /// 删除本地文件
        /// </summary>
        /// <param name="url"></param>
        public static void DeleteTheLocal(string url)
        {
            //https://localhost:44376/qrcode_20250114_103734_7da57c2d-e027-4d59-860b-a555ef71fde9.jpg
            //D:\BitterCandy\factory\Factory\src\Factory.Web\wwwroot\qrcode\qrcode_20250114_103734_7da57c2d-e027-4d59-860b-a555ef71fde9.jpg
            //D:\BitterCandy\factory\Factory\src\Factory.Web\wwwroot\qrcode\qrcode_20250113_204117_9712a04b-de6e-449a-bce0-dfdafeb36ca2.jpg
            var imgUrl = url.Substring(24);
            //var DeleteUrl = "D:\\BitterCandy\\factory\\Factory\\src\\Factory.Web\\wwwroot\\qrcode\\" + imgUrl;
            var DeleteUrl = Environment.CurrentDirectory + "\\wwwroot\\qrcode\\" + imgUrl;
            File.Delete(DeleteUrl);
        }

    }
}
